package net.stratusprogramming.school.Game.World;

import net.stratusprogramming.school.Game.Battle.Battle;
import net.stratusprogramming.school.Game.Combatants.MrHayes;
import net.stratusprogramming.school.Game.Framework.Screen;
import net.stratusprogramming.school.Game.Framework.Scene.Camera;
import net.stratusprogramming.school.Game.Framework.Scene.Group;
import net.stratusprogramming.school.Game.Framework.System.InstanceMgr;
import net.stratusprogramming.school.Game.Framework.World.GameObjectMgr;
import net.stratusprogramming.school.Game.Screens.InventoryScreen;
import net.stratusprogramming.school.Game.Screens.PauseScreen;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;

public class World extends Screen {

	private Input input;
	private Group root;
	protected Camera camera;

	public World() {
		input = InstanceMgr.input;
		init();
	}

	public void init() { 
		camera = new Camera();
		InstanceMgr.camera = camera;
		InstanceMgr.map = new Map();
		root = new Group();
		root.add(new GroundRenderer());
		root.add(new Player());
		root.add(new ObstRenderer());
		root.add(new CeilRenderer());		
		root.add(new GameObjectMgr());
	}

	public void save() {
		InstanceMgr.map.save(InstanceMgr.saveMgr);
		camera.save(InstanceMgr.saveMgr);
		root.save(InstanceMgr.saveMgr);

	}

	public void load() {
		InstanceMgr.map.load(InstanceMgr.saveMgr);
		camera.load(InstanceMgr.saveMgr);
		root.load(InstanceMgr.saveMgr);
	}

	public void update(int delta) {
		root.update(delta);
		camera.update(delta);

		if(input.isKeyPressed(Input.KEY_P) || input.isKeyPressed(Input.KEY_ESCAPE)) {
			InstanceMgr.main.setScreen(new PauseScreen(this));
		}
		
		if(input.isKeyPressed(Input.KEY_B))	{
			InstanceMgr.main.setScreen(new Battle(new MrHayes()));
		}
		
		if(input.isKeyPressed(Input.KEY_I)) {
			InstanceMgr.main.setScreen(new InventoryScreen(this));
		}
	}

	public void render(Graphics g) {
		root.render(g);
	}

}
